import { ADD_COUNTER, ADD_TO_CART, ADD_CART } from "store/mutation-types"


export default {
  [ADD_CART](context, payload) {
    return new Promise((resolve, reject) => {

      // 查找之前数组中是否存在该商品
      let oldProduct = context.state.cartList.find(item => item.iid === payload.iid)

      // 如果存在，商品数量+1，不存在将商品添加到cartList中，并将count值初始化为1
      if (oldProduct) {
        context.commit(ADD_COUNTER, oldProduct)
        resolve("商品数量+1")
      }
      else {
        context.commit(ADD_TO_CART, payload)
        resolve("添加商品成功！")
      }

    })
  }
}
